package com.lq.memo.task;

import com.lq.email.exception.MyException;
import com.lq.memo.config.MemoConfig;
import com.lq.memo.entity.Memo;
import com.lq.memo.service.MemoService;
import com.lq.memo.service.UserService;
import com.lq.memo.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;

/**
 * @program: memo
 * @description: 定时任务
 * @author: LQ
 * @create: 2021-10-28 11:30
 */

//1.主要用于标记配置类，兼备Component的效果。
@Configuration
// 2.开启定时任务
@EnableScheduling
public class EmailTask {

    @Autowired
    MemoService memoService;
    @Autowired
    UserService userService;
    @Autowired
    MemoConfig memoConfig;
    @Autowired
    DateUtil dateUtil;

    // @Autowired
    // private EmailService emailService;
    // cron语法说明
    // 共 7 位，最后一位可选，可以不写，至少 6 位，从左到右各位置分别是：
    //位置	意义	取值	支持的符号
    //1	秒	0-59	, - * /
    //2	分	0-59	, - * /
    //3	时	0-23	, - * /
    //4	日	1-31	, - * ? / L W C
    //5	月	1-12 或 JAN - DEC	, - * /
    //6	周	1-7 或 MON - SAT	, - * ? / L C #
    //7	年	空或 1970-2099	, - * /
    @Scheduled(cron = "0 0 9 * * ? ")
    public void configureEmailTasks9H() throws MyException {
        List<Memo> memoList = memoService.getMemoList(memoConfig.getBeforeDate(), memoConfig.getAfterDate());
        memoService.sendEmailMemo(memoList);
    }
    @Scheduled(cron = "0 0 7 * * ? ")
    public void configureEmailTasks7H() throws MyException {
        List<Memo> memoList = memoService.getMemoList(memoConfig.getBeforeDate(), memoConfig.getAfterDate());
        memoService.sendEmailMemo(memoList);
    }
    @Scheduled(cron = "0 0 10 * * ? ")
    public void configureEmailTasks10H() throws MyException {
        List<Memo> memoList = memoService.getMemoList(memoConfig.getBeforeDate(), memoConfig.getAfterDate());
        memoService.sendEmailMemo(memoList);
    }
}
